package com.ruoyi.jpjstb.service.param.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.jpjstb.domain.common.Cost;
import com.ruoyi.jpjstb.domain.common.Dept;
import com.ruoyi.jpjstb.domain.common.Month;
import com.ruoyi.jpjstb.mapper.CostMapper;
import com.ruoyi.jpjstb.service.CostService;
import com.ruoyi.jpjstb.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CostServiceimpl implements CostService {
    @Autowired
    CostMapper costMapper;

    @Override
    public List<Cost> selectCostList(Cost cost) {
        return costMapper.selectCostList(cost);
    }

    @Override
    public int deleteCostByIds(String ids) {
        return costMapper.deleteCostByIds(Convert.toStrArray(ids));
    }

    @Override
    public int insertCost(Cost cost) {
        return costMapper.insertCost(cost);
    }

    public List<Month> findmonth(Month month) {

        return costMapper.findmonth(month);
    }

    @Override
    public Cost selectCostById(Long id) {
        return costMapper.selectCostById(id);
    }

    @Override
    public int updateCost(Cost cost) {
        return costMapper.updateCost(cost);
    }

    public List<Dept> findDept(Dept dept) {

        return costMapper.findDept(dept);
    }

    @Override
    public int importData(MultipartFile file) {
        //获取Excel数据  0节点为所有项目名 为String数组
        int num = 0;
        List<String[]> excelData = ExcelUtils.getExcelData(file);
        Map<String, String> fieldMap = new HashMap<String, String>();
        fieldMap.put("人数", "number");
        fieldMap.put("工资", "wages");
        fieldMap.put("油补", "oilPatch");
        fieldMap.put("社保", "socialSecurity");
        fieldMap.put("公积金", "accumulationFund");
        fieldMap.put("费用合计", "totalCost");
        fieldMap.put("月份", "month");
        fieldMap.put("一级部门", "dept");


        for (int i = 1; i < excelData.size(); i++) {
            Map<String, String> addMap = new HashMap<String, String>();
            for (int j = 0; j < excelData.get(i).length; j++) {
                String val = excelData.get(i)[j];
                //取出对应的字段
                String field = excelData.get(0)[j];
//                if (field.equals("电话")) {
//                    if (!val.equals("")) {
//                        val = val.replace(",", "");
//                    }
//                }
                addMap.put(fieldMap.get(field), val);
            }
            System.out.println(addMap.size());
            num += costMapper.insertData(addMap);
        }
        return num;
    }


}
